<?php
class UsersController extends Zend_Controller_Action
{
 	protected $_users;
 	protected $_tickets;
 	protected $_form;

	public function init()
    {
        $this->_users = new Application_Model_Users();
        $this->_tickets = new Application_Model_Tickets();
        $this->_form = new Application_Form_User();
    }

	public function  preDispatch()
    {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
                $this->_helper->redirector('index', 'login');
        }
    }

    public function editAction()
    {
        $this->view->title = 'Редактиране на потребител';
        $this->view->headTitle($this->view->title);

    	if (!$this->_helper->isLogged()) {
    		$this->_helper->redirector('notfound', 'error');
    	}

    	$this->_form->removeElement('username');
    	$this->_form->getElement('submit')->setLabel('Редактирай');
    	$this->_form->getElement('id')->setValue($this->_helper->getLoggedUser());

    	if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();
            if ($this->_form->isValid($formData)) {
                $this->_users->editUser($this->_form->getValues());
                $this->_helper->log("Редактиране на потребителски профил");
                $this->_helper->redirector('index');
            } else {
                $this->_form->populate($formData);
            }

        } else {

            $id = $this->_helper->getLoggedUser();
            try {
                $this->_form->populate($this->_users->getUser($id));
            } catch (Exception $e) {
                $this->_helper->redirector('notfound', 'error');
            }

        }

    	$this->view->form = $this->_form;
    }

    public function indexAction()
    {
        $this->view->title = 'Потребители';
        $this->view->headTitle($this->view->title);

        $this->view->users = $this->_users->getUserList();
    }

    public function viewAction()
    {
        $this->view->title = 'Преглед на потребител';
        $this->view->headTitle($this->view->title);

        $id = $this->_getParam('id', 0);

        try {
            $logs = new Application_Model_Logs();
            $user = $this->_users->getUser($id);
            $user['last_action'] = $logs->getLastLogByUser($id);
            $this->view->user = $user;

            $this->view->tasks = $this->_users->getUserTasks($id);

            $this->_helper->log("Преглед на потребител $id");
        } catch (Exception $ex) {
            $this->_helper->redirector('notfound', 'error');
        }
    }

    public function deleteAction()
    {
        $this->view->title = 'Изтриване на потребител';
        $this->view->headTitle($this->view->title);

        $id = $this->_getParam('id', 0);

        try {
            $user = $this->_users->getUser($id);

            if ($this->getRequest()->isPost()) {
                if ($this->getRequest()->getPost('btn') == 'Да') {
                    $this->_users->deleteUser($id);
                }
                $this->_helper->log("Изтриване на потребител $id");
                $this->_helper->redirector('index');
            } else {
                $this->view->user = $user;
            }
        } catch (Exception $ex) {
            $this->_helper->redirector('notfound', 'error');
        }
    }


}